// caelan
// 516 --
// SpecialTopic
'use strict';

var SpecialTopic = {
    layer: null,
    init: function( param ) {
        if ( !SuperMap ) { console.log( '缺少SuperMap Api, 无法实例化功能项!' ); return false; }
        if ( typeof SuperMap !== 'object' ) { console.log( '缺少SuperMap Api, 无法实例化功能项!' ); return false; }
        if ( !param.url ) { console.log( '缺少数据图层路径参数!' ); return false; }

        SpecialTopic.create( param );
    },
    create: function( param ) {
        SpecialTopic.removeLayer( param );

        SpecialTopic.layer = new SuperMap.Layer.Range( "专题图" );
        SpecialTopic.layer.setOpacity( 0.8 );

        SpecialTopic.layer.style = { fillColor: "#FFFFFF" };

        SpecialTopic.layer.isHoverAble = true;
        SpecialTopic.layer.highlightStyle = {
            stroke: true,
            strokeWidth: 1,
            strokeColor: '#00b050',
            fillColor: '#5bffa6',
            fillOpacity: 0.8
        };

        SpecialTopic.layer.themeField = "area";

        if ( param.colors && param.colors.length > 0 ) {
            SpecialTopic.layer.styleGroups = param.colors;
        } else {
            SpecialTopic.layer.styleGroups = [
                { start: 0, end: 0.02, style: { color: '#FDE2CA' } },
                { start: 0.02, end: 0.04, style: { color: '#FACE9C' } },
                { start: 0.04, end: 0.06, style: { color: '#F09C42' } },
                { start: 0.06, end: 0.1, style: { color: '#D0770B' } },
                { start: 0.1, end: 0.2, style: { color: '#945305' } }
            ];
        }

        SpecialTopic.layer.on( "click", function( e ) {
            if (e.target && e.target.refDataID) {
                var fea = SpecialTopic.layer.getFeatureById( e.target.refDataID );
                if ( fea ) {
                    
                }
            }
        } );

        SpecialTopic.layer.on( "mousemove", function( e ) {
            if ( e.target && e.target.refDataID ) {
                var fea = SpecialTopic.layer.getFeatureById( e.target.refDataID );
                if ( fea && fea.attributes.area > 0 ) {
                    $("#marker_").remove();
                    var markerPop = new SuperMap.InfoWindow( "marker_" );
                    markerPop.titleBox = false;
                    markerPop.contentSize = new SuperMap.Size( 180, 60 );
                    markerPop.render();

                    var latlng = param.map.getLonLatFromPixel({ x: e.event.x, y: e.event.y });
                    markerPop.setLonLat( latlng, { x: 0, y: 0 } );

                    markerPop.setContentHTML( null, param.callbackHtml( fea ) );

                    param.map.addPopup( markerPop );

                    var top = $("#marker_")[0].offsetTop;

                    $("#marker__header").remove();
                } else {
                    $("#marker_").remove();
                }
            } else {
                $("#marker_").remove();

            }
        } );

        param.map.addLayers([ SpecialTopic.layer ]);

        var getFeatureParam = new SuperMap.REST.FilterParameter({
            name: param.dataSourceName,
            attributeFilter: param.dataSourceFilter
        });

        var getFeatureBySQLParams = new SuperMap.REST.GetFeaturesBySQLParameters({
            queryParameter: getFeatureParam,
            toIndex: 500,
            datasetNames: param.datasetNames
        });

        var getFeatureBySQLService = new SuperMap.REST.GetFeaturesBySQLService( param.url, {
            eventListeners: {
                "processCompleted": function( getFeaturesEventArgs ) {
                    var result = getFeaturesEventArgs.result;
                    if ( result && result.features ) {
                        SpecialTopic.layer.addFeatures( result.features );
                        SpecialTopic.updateData( param );
                    }
                },
                "processFailed": function( e ) {
                    console.log( e.error.errorMsg );
                }
            }
        });

        getFeatureBySQLService.processAsync( getFeatureBySQLParams );
    },
    updateData: function( param ) {
        var feas = SpecialTopic.layer.features;

        for (var i = 0, len = feas.length; i < len; i++) {
            var fea = feas[i],
                feaName = fea.attributes.NAME;

            for ( var x = 0, lent = param.items.length; x < lent; x++ ) {
                if ( feaName.indexOf( param.items[x].name ) > -1 ) {
                    fea.attributes['area'] = param.items[x].value;
                    break;
                }
            }
        }

        SpecialTopic.layer.redraw();
    },
    removeLayer: function( param ) {
        if ( SpecialTopic.layer ) {
            if ( param.map.getLayersByName("专题图").length > 0 ) {
                param.map.removeLayer( SpecialTopic.layer );
            }
            SpecialTopic.layer = null;
        }
    }
};

var specialTopic = function( param ) { return new SpecialTopic.init( param ); };